#!/bin/sh
#
# Squish a CM otapackage for distribution
# cyanogen / CyanogenDefy / Atrix-Dev-Team
#

echo "DEFY SQUISHER"
echo "================"

DEVICE_OUT=$ANDROID_BUILD_TOP/out/target/product/mb525
DEVICE_TOP=$ANDROID_BUILD_TOP/device/moto/mb525
VENDOR_TOP=$ANDROID_BUILD_TOP/vendor/motorola/jordan

CL_PFX="\033[33m"
CL_INS="\033[36m"
CL_RST="\033[0m"

OUT_TARGET_HOST=`uname -a | grep Darwin`
if [ -z "$OUT_TARGET_HOST" ]
then
   OUT_TARGET_HOST=linux-x86
   MD5=md5sum
   XARGS="xargs --max-args=1 --max-procs `grep 'processor' /proc/cpuinfo|wc -l`"
   SED=sed
else
   OUT_TARGET_HOST=darwin-x86
   MD5=md5
   XARGS="xargs -n 1 -P `sysctl hw.ncpu | awk '{print $2}'`"
   SED=gsed
fi

if [ -z "$OUT" -o ! -d "$OUT" ]; then
	echo "ERROR: $0 only works with a full build environment. $OUT should exist."
	exit 1
fi

if [ "$TARGET_BUILD_TYPE" = "debug" ]; then
    OTAPACKAGE=$OUT/${TARGET_PRODUCT}_debug-ota-$TARGET_BUILD_VARIANT.$USER.zip
elif [ "$TARGET_BUILD_VARIANT" = "userdebug" ]; then
    OTAPACKAGE=$OUT/$TARGET_PRODUCT-ota-eng.$USER.zip
else
    OTAPACKAGE=$OUT/$TARGET_PRODUCT-ota-$TARGET_BUILD_VARIANT.$USER.zip
fi
if [ ! -f "$OTAPACKAGE" ]; then
	echo "$OTAPACKAGE doesn't exist!"
	exit 1
fi

OPTICHARGER=$DEVICE_TOP/releasetools/opticharger
QUIET=-q
DELETE_BINS="applypatch applypatch_static check_prereq recovery updater"


REPACK=$OUT/repack.d
printf "Sanitizing environment..."
rm -rf $REPACK
mkdir -p $REPACK
echo

# Unpack the otapackage and opticharge all apks
mkdir $REPACK/ota
(
cd $REPACK/ota
printf "Unpacking $OTAPACKAGE..."
unzip $QUIET $OTAPACKAGE
echo
cd $REPACK/ota/system/framework
)

# Include device specific script
if [ -f $DEVICE_TOP/releasetools/squisher.sh ]; then
       . $DEVICE_TOP/releasetools/squisher.sh
fi

if [ "$1" != "fast" ]; then
	$OPTICHARGER framework-res.apk
	cd $REPACK/ota/system/app
	find ./ -name \*.apk | $XARGS $OPTICHARGER
else
	echo "skipping apk optimization"
fi

# Fix build.prop
$SED -i \
	-e '/ro\.kernel\.android\.checkjni/d' \
	-e '/ro\.build\.type/s/eng/user/' \
	$REPACK/ota/system/build.prop

# Delete unnecessary binaries
if [ -n "$DELETE_BINS" ]; then

	echo "Removing unwanted binaries..."
	cd $REPACK/ota/system/bin && echo $DELETE_BINS | xargs /bin/rm -f
fi

cd $REPACK/ota/system/bin && find -name "*_test" -exec /bin/rm -f {} \;
cd $REPACK/ota/system/bin && find -name "*_Test" -exec /bin/rm -f {} \;
cd $REPACK/ota/system/bin && find -name "test_*" -exec /bin/rm -f {} \;
cd $REPACK/ota/system/bin && find -name "*Test*" -exec /bin/rm -f {} \;
cd $REPACK

# Strip modules
[ -d $REPACK/ota/system/lib/modules ] && \
	find $REPACK/ota/system/lib/modules -name "*.ko" -not -name "overclock*" -print0 | xargs -0 arm-eabi-strip --strip-unneeded

# Determine what to name the new signed package
MODVERSION=`sed -n -e'/ro\.modversion/s/^.*=//p' $REPACK/ota/system/build.prop`
: ${MODVERSION:=nightly}

MODVERSION=`echo $MODVERSION | sed 's/CyanogenMod-//'`
OUTFILE=$OUT/update-cm-$MODVERSION-signed.zip

if [ -n "$CM_NIGHTLY" ]; then
	OUTFILE=$OUT/CM$MODVERSION.zip
fi

# Pack it up and sign
echo "Zipping package..."
( cd $REPACK/ota; zip $QUIET -r $REPACK/update.zip . )
echo
echo "Signing package..."
SECURITYDIR=$ANDROID_BUILD_TOP/build/target/product/security
java -Xmx768m \
	-jar $ANDROID_BUILD_TOP/out/host/$OUT_TARGET_HOST/framework/signapk.jar \
	-w $SECURITYDIR/testkey.x509.pem $SECURITYDIR/testkey.pk8 \
	$REPACK/update.zip $OUTFILE
echo
echo "Cleaning up..."
rm -rf $REPACK
echo

# Create a md5 checksum image of the repacked package
(
img=`basename $OUTFILE`
cd `dirname $OUTFILE`
$MD5 $img >$img.md5sum
echo
echo -e ${CL_INS}"Package complete: $OUTFILE" ${CL_RST}
cat $img.md5sum
echo
# Build date and current time:
cat $OUT/system/build.prop | grep ro.build.date=
ls -l $OUTFILE
echo
)

if [ -x $DEVICE_TOP/releasetools/addons ]; then
	$DEVICE_TOP/releasetools/addons kernel
#	$DEVICE_TOP/releasetools/addons bootmenu
#	$DEVICE_TOP/releasetools/addons twrp-recovery
fi

exit 0
